<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/postedit' }"
        >商品管理</el-breadcrumb-item
      >
      <el-breadcrumb-item>商品更新</el-breadcrumb-item>
    </el-breadcrumb>

    <el-form ref="form" :model="form" label-width="80px" class="table">
      <el-form-item label="商品id">
        <el-input v-model="form.goodsId"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="form.goodsName"></el-input>
      </el-form-item>
      <el-form-item label="商品价格">
        <el-input v-model="form.goodsPrice"></el-input>
      </el-form-item>

      <el-form-item label="商品图片">
        <el-upload
        class="avatar-uploader"
        action="https://jsonplaceholder.typicode.com/posts/"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :headers="{ 'Content-Type': 'application/x-www-form-urlencoded'}"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar" >
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <!-- <el-upload
        action="https://jsonplaceholder.typicode.com/posts/"
        list-type="picture-card"
        :on-preview="handlePictureCardPreview"
        :on-success="handlePictureSuccess"
        :before-upload="beforeAvatarUpload"
        :on-remove="handleRemove"
        :headers="{ 'Content-Type': 'application/x-www-form-urlencoded' }"
        :limit="1"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt=""  v-if="dialogImageUrl"/>
      </el-dialog> -->
      <!-- 上传图片 -->

      <el-form-item label="商品简介" class="goodsinfo">
        <el-input
          type="textarea"
          v-model="form.goodsIntro"
          class="textarea"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即更新</el-button>
        <el-button @click="goback">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ["id"],
  data() {
    return {
      form: {
        goodsName: "",
        goodsPrice: "",
        goodsIntro: "",
        goodsImg:"",
        goodsId:""
      },
      imageUrl: "",
      dialogVisible: false,
      file: {},
      // imageUrl:'',
    };
  },
  methods: {
    onSubmit() {
      this.$axios
        .post(
          "/api/store/update",
          {
            goodsName: this.form.goodsName,
            goodsPrice: this.form.goodsPrice,
            goodsIntro: this.form.goodsIntro,
          },
          {
            params: { goodsId: this.id },
          }
        )
        .then((res) => {
          if (res.data.code == 0) {
            this.$message({
              message: "更新成功",
              type: "success",
            });
          } else {
            this.$message({
              message: "更新失败，请重新操作",
              type: "error",
            });
          }
        })
        .catch((err) => {
          this.$message({
            message: "更新失败，请重新操作",
            type: "error",
          });
        });
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.imageUrl = file.url;
      this.dialogVisible = true;
    },
    handleAvatarSuccess(response, file, fileList) {
      this.file = file.raw;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 8;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    goback(){
      this.$router.back()
    }
  },
  mounted(){///数据回显
   this.$axios.get('/api/store/detail',{
      params:{goodsId :this.id}
    }).then((res)=>{
      if(res.data.code == 0){
        this.form = res.data.infos;
        this.imageUrl = this.form.goodsImg;
        
      }
    }).catch((err)=>{
      this.$message({
                message: '更新失败，请重新操作',
                type: 'error'
            });
            this.$router.back();
    })

  }
};
</script>

<style lang="scss" scoped>
.table {
  margin: 20px;
  padding: 60px;
  width: 60%;
  background: white;
  opacity: 0.9;
}
.textarea {
  min-height: 60px;
}
.goodsinfo{
  margin-top: 20px;
}
.avatar{
  width: 200px;
}
</style>